package com.yto.hrreport.event {
	
	import com.adobe.serialization.json.JSON;
	import com.yto.hrreport.command.ReportCommand;
	import com.yto.hrreport.command.sysTemplatePanel;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.containers.HDividedBox;
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	import mx.controls.TextInput;
	import mx.controls.Tree;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.events.ListEvent;
	import mx.events.TreeEvent;
	import mx.managers.PopUpManager;
	import mx.managers.SystemManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import spark.components.Label;
	
	public class HrreportEventHandler {
		
		public var curSelectItem : Object;
		public var sysCurSelectItem : Object;
		public var headGrid : DataGrid;
		public var bodyGrid : DataGrid;
		public var headCanvas : Canvas;
		public var sysTree : Tree;
		private var buttonEvent : ButtonEvent;
		private var template : sysTemplatePanel;
		private var parentName : String;

		private static var handler : HrreportEventHandler = new HrreportEventHandler();
		
		public function HrreportEventHandler() {
			
		}
		
		public static function getInstance() : HrreportEventHandler {
			return handler;
		}
		
		public function leftTree_itemDoubleClickHandler(event: Event):void {
			
			var tree : Tree = Tree(event.target);
			
			if(tree.selectedItem.isparent == "Y") {
				
				curSelectItem = tree.selectedItem;
				
				if(curSelectItem.children == null || curSelectItem.children == "[]")
					leftTree_update(tree);
				else {
					tree.selectedItem.isopen = !tree.isItemOpen(tree.selectedItem);
					tree.expandItem(tree.selectedItem , !tree.isItemOpen(tree.selectedItem));
					
				}
				
				
				return ;
			}
			
			if(tree.name.split(".")[0] == "system")
				return ;
			
			if(hrreport.ui.mainNavigator.getChildByName(tree.selectedItem.nodeid + "_canvas") != null) {
				hrreport.ui.mainNavigator.selectedIndex = hrreport.ui.mainNavigator.getChildIndex(hrreport.ui.mainNavigator.getChildByName(tree.selectedItem.nodeid + "_canvas"));
				return ;
			}
			
			var canvas : Canvas = new Canvas();
			canvas.name = tree.selectedItem.nodeid + "_canvas";
			canvas.percentHeight = 100;
			canvas.percentWidth = 100;
			canvas.label = tree.selectedItem.nodename;
			
			var bodyCanvas : Canvas = new Canvas();
			bodyCanvas.name = tree.selectedItem.nodeid + "_bodyCanvas";
			bodyCanvas.bottom = 5;
			bodyCanvas.top = 60;
			bodyCanvas.left = 5;
			bodyCanvas.right = 5;
			
			if(String(tree.selectedItem.nodeid).split(".")[0] == "system") 
				createBodyCanvasBySystem(bodyCanvas , tree);
			else
				createBodyCanvas(bodyCanvas , tree);
			
			var headCanvas : Canvas = new Canvas();
			headCanvas.name = tree.selectedItem.nodeid + "_headCanvas";
			headCanvas.top = 5;
			headCanvas.height = 300;
			headCanvas.left = 5;
			headCanvas.right = 5;
			ButtonEventHandler.getInstance().initButtons(headCanvas , tree);
			
			canvas.addChild(headCanvas);
			canvas.addChild(bodyCanvas);
			
			hrreport.ui.mainNavigator.addElement(canvas);
			hrreport.ui.mainNavigator.selectedIndex = hrreport.ui.mainNavigator.getChildIndex(canvas);
			
		}
		
		
		public function resultHandler(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null ) {
				var result : Array = JSON.decode(lastResult) as Array;
				
				for each(var item : Object in result) {
					if(item.isparent == "Y" && item.children == null)
						item.children = "[]";
				}
				
				var nodeArray : ArrayCollection = new ArrayCollection(result);
				hrreport.ui.leftTree.dataProvider = nodeArray;
				
			} 
			
			var btnRefresh : Button = new Button();
			btnRefresh.name = "leftRefresh";
			btnRefresh.right = 5;
			btnRefresh.top = 5;
			btnRefresh.width = 80;
			btnRefresh.label = "刷新树";
			hrreport.ui.leftTree.addChild(btnRefresh);
			
		}
		
		public function sysTreeClickHandler(event : ListEvent) : void {
			
			var tree : Tree = Tree(event.target);
			var parentName : String = tree.name.split("_")[0];
			
			if(sysCurSelectItem == null) {
				sysCurSelectItem = new Object();
				sysCurSelectItem.nodeid = "";
			}
			
			if(sysCurSelectItem.nodeid == null) {
				sysCurSelectItem.nodeid = "";
			}
			
			if(sysCurSelectItem.nodeid != tree.selectedItem.nodeid) {
				sysCurSelectItem = tree.selectedItem;
				
				sysPanelEvent(parentName); 
				
			}
			
		} 
		
		public function sysPanelEvent(parentName : String) : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			var sysTree : Tree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));
			var sysPanel : Panel = Panel(hdivBox.getChildByName(parentName + "_sysFunc"));
			
			if(buttonEvent == null)
				buttonEvent = new ButtonEvent();
			
			if(sysPanel == null) {
				templatePanel(sysTree);
				buttonEvent.updateButtons(null , "sysTree_ItemChange_template" , headCanvas , sysTree.selectedItem.isparent == "Y" ? false : true);
				
			} else {
				var sysCanvas : Canvas = Canvas(sysPanel.getElementAt(0));
				
				var txtNodeCode : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeCode"));
				var txtNodeId : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeId"));
				var txtNodeName : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeName"));
				var txtParentNodeCode : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtParentNodeCode"));
				var chkIsParent : CheckBox = CheckBox(sysCanvas.getChildByName(parentName + "_chkIsParent"));
				
				if(sysTree.selectedItem != null) {
					
					var item : Object = sysTree.selectedItem;
					
					txtNodeCode.text = item.nodecode;
					txtNodeId.text = item.nodeid;
					txtNodeName.text = item.nodename;
					txtParentNodeCode.text = item.parentcode;
					chkIsParent.selected = item.isparent == "Y" ? true : false;
					
				}
				
				buttonEvent.updateButtons(null , "sysTree_ItemChange" , headCanvas , sysTree.selectedItem.isparent == "Y" ? false : true);
				
				if(sysTree.selectedItem.children != null && sysTree.selectedItem.children == "[]")
					Button(headCanvas.getChildByName(parentName + "_btnDelete")).enabled = true;
				
			}
			
		}
		
		public function headPosGridResult(event : ResultEvent) : void {
			var lastResult : String = event.result as String;
			
			if(lastResult != null) {
				var result : Array = JSON.decode(lastResult) as Array;
				var headArray : ArrayCollection = new ArrayCollection(result);
				template.headPosGrid.dataProvider = headArray;
			} else 
				template.headPosGrid.dataProvider = null;
			
		}
		
		public function bodyPosGridResult(event : ResultEvent) : void {
			var lastResult : String = event.result as String;
			
			if(lastResult != null) {
				var result : Array = JSON.decode(lastResult) as Array;
				var bodyArray : ArrayCollection = new ArrayCollection(result);
				template.bodyPosGrid.dataProvider = bodyArray;
			} else 
				template.bodyPosGrid.dataProvider = null;
			
			PopUpManager.removePopUp(hrreport.waitWindow);
			
			if(parentName != null) {
				
				var head : ArrayCollection = ArrayCollection(template.headPosGrid.dataProvider);
				var body : ArrayCollection = ArrayCollection(template.bodyPosGrid.dataProvider);
				
				if((head == null || head.length == 0) && (body == null || body.length == 0)) {
					Button(headCanvas.getChildByName(parentName + "_btnAdd")).enabled = true;
					Button(headCanvas.getChildByName(parentName + "_btnEdit")).enabled = false;
					Button(headCanvas.getChildByName(parentName + "_btnDelete")).enabled = false;
				} else {
					Button(headCanvas.getChildByName(parentName + "_btnAdd")).enabled = false;
					Button(headCanvas.getChildByName(parentName + "_btnEdit")).enabled = true;	
					Button(headCanvas.getChildByName(parentName + "_btnDelete")).enabled = true;
				}
			}
		}
		
		public function templatePanel(sysTree : Tree) : void {
			
			parentName = sysTree.name.split("_")[0];
			
			var item : Object = sysTree.selectedItem;
			
			if(item.isparent != "Y") {
				var tableServer : RemoteObject = new RemoteObject;
				tableServer.destination = IProcessor.TableProcessor;
				tableServer.resultTableJSON(item.nodeid , "head");
				tableServer.addEventListener(ResultEvent.RESULT , headPosGridResult);
				
				var tableBodyServer : RemoteObject = new RemoteObject;
				tableBodyServer.destination = IProcessor.TableProcessor;
				tableBodyServer.resultTableJSON(item.nodeid , "body");
				tableBodyServer.addEventListener(ResultEvent.RESULT , bodyPosGridResult);
				
				PopUpManager.addPopUp(hrreport.waitWindow , hrreport.ui , true);
				PopUpManager.centerPopUp(hrreport.waitWindow);
			} else {
				template.headPosGrid.dataProvider = null;
				template.bodyPosGrid.dataProvider = null;
			}
			
			template.txtFieldCode.text = null;
			template.txtFieldName.text = null;
			template.combPos.selectedIndex = -1;
			template.btnAddRow.enabled = false;
			template.btnDelRow.enabled = false;
			
		}
		
		public function createBodyCanvasBySystem(bodyCanvas : Canvas , tree : Tree) : void {
			
			var parentName : String = bodyCanvas.name.split("_")[0];
			
			var hdivBox : HDividedBox = new HDividedBox();
			hdivBox.name = parentName + "_hdivBox";
			hdivBox.percentHeight = 100;
			hdivBox.percentWidth = 100;
			
			sysTree = new Tree();
			sysTree.name = parentName + "_sysTree";
			sysTree.width = 200;
			sysTree.maxWidth = 400;
			sysTree.percentHeight = 100;
			sysTree.labelField = "nodename";
			sysTree.doubleClickEnabled = true;
			sysTree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK , leftTree_itemDoubleClickHandler);
			sysTree.addEventListener(ListEvent.ITEM_CLICK , sysTreeClickHandler);
			sysTree.addEventListener(TreeEvent.ITEM_OPENING , leftTree_itemOpeningHandler);
			
			var dataProvider : ArrayCollection = hrreport.ui.leftTree.dataProvider as ArrayCollection;
			var newProvider : ArrayCollection = new ArrayCollection();
			for each(var item : Object in dataProvider) {
				
				// 过滤掉系统的节点
				if(String(item.nodeid).split(".")[0] == "system")
					continue;
				
				item.isopen = false;
				newProvider.addItem(item);
			}
			
			sysTree.dataProvider = newProvider;
			sysTree.selectedIndex = 0;
			sysTree.expandChildrenOf(sysTree.selectedItem , false);
			
			var sysPanel : Panel = new Panel();
			
			var funcName : String = parentName.split(".")[1];
			
			if(funcName != null) {
				if(funcName == "funcregister") {
					sysPanel.name = parentName + "_sysFunc";
					sysPanel.percentHeight = 100;
					sysPanel.percentWidth = 100;
					sysPanel.title = "节点信息";
					createSysFunc(sysPanel);
				} else if(funcName == "templateregister") {
					sysPanel.name = parentName + "_sysTemplate";
					sysPanel.percentHeight = 100;
					sysPanel.percentWidth = 100;
					sysPanel.title = "报表模板";
					createSysTemplate(sysPanel);
					
				}
			}
			
			hdivBox.addChild(sysTree);
			hdivBox.addChild(sysPanel);
			
			bodyCanvas.addChild(hdivBox);
		}
		
		public function createSysTemplate(sysPanel : Panel) : void {
			
			template = new sysTemplatePanel();
			sysPanel.addElement(template);
		}
		
		public function createSysFunc(sysPanel : Panel) : void {
			
			var parentName : String = sysPanel.name.split("_")[0];
			
			var canvas : Canvas = new Canvas();
			canvas.name = parentName + "_sysCanvas";
			canvas.percentHeight = 100;
			canvas.percentWidth = 100;
			
			var lblNodeCode : Label = new Label();
			lblNodeCode.name = parentName + "_lblNodeCode";
			lblNodeCode.text = "节点号";
			lblNodeCode.top = 13;
			lblNodeCode.left = 10;
			lblNodeCode.width = 60;
			
			var lblNodeId : Label = new Label();
			lblNodeId.name = parentName + "_lblNodeId";
			lblNodeId.text = "节点编码";
			lblNodeId.top = 46;
			lblNodeId.left = 10;
			lblNodeId.width = 60;
			
			var lblNodeName : Label = new Label();
			lblNodeName.name = parentName + "_lblNodeName";
			lblNodeName.text = "节点名称";
			lblNodeName.top = 79;
			lblNodeName.left = 10;
			lblNodeName.width = 60;
			
			var lblParentNodeCode : Label = new Label();
			lblParentNodeCode.name = parentName + "_lblParentNodeCode";
			lblParentNodeCode.text = "上级节点号";
			lblParentNodeCode.top = 109;
			lblParentNodeCode.left = 10;
			lblParentNodeCode.width = 60;
			
			var lblIsParent : Label = new Label();
			lblIsParent.name = parentName + "_lblIsParent";
			lblIsParent.text = "是否上级";
			lblIsParent.top = 135;
			lblIsParent.left = 10;
			lblIsParent.width = 60;
			
			var txtNodeCode : TextInput = new TextInput();
			txtNodeCode.name = parentName + "_txtNodeCode";
			txtNodeCode.top = 10;
			txtNodeCode.left = 80;
			txtNodeCode.percentWidth = 80;
			txtNodeCode.enabled = false;
			
			var txtNodeId : TextInput = new TextInput();
			txtNodeId.name = parentName + "_txtNodeId";
			txtNodeId.top = 40;
			txtNodeId.left = 80;
			txtNodeId.percentWidth = 80;
			txtNodeId.enabled = false;
			
			var txtNodeName : TextInput = new TextInput();
			txtNodeName.name = parentName + "_txtNodeName";
			txtNodeName.top = 70;
			txtNodeName.left = 80;
			txtNodeName.percentWidth = 80;
			txtNodeName.enabled = false;
			
			var txtParentNodeCode : TextInput = new TextInput();
			txtParentNodeCode.name = parentName + "_txtParentNodeCode";
			txtParentNodeCode.top = 100;
			txtParentNodeCode.left = 80;
			txtParentNodeCode.percentWidth = 80;
			txtParentNodeCode.enabled = false;
			
			var chkIsParent : CheckBox = new CheckBox();
			chkIsParent.name = parentName + "_chkIsParent";
			chkIsParent.top = 130;
			chkIsParent.left = 80;
			chkIsParent.enabled = false;
			
			
			canvas.addChild(lblNodeCode);
			canvas.addChild(lblNodeId);
			canvas.addChild(lblNodeName);
			canvas.addChild(lblParentNodeCode);
			canvas.addChild(lblIsParent);
			
			canvas.addChild(txtNodeCode);
			canvas.addChild(txtNodeId);
			canvas.addChild(txtNodeName);
			canvas.addChild(txtParentNodeCode);
			canvas.addChild(chkIsParent);
			
			sysPanel.addElement(canvas);
			
		}
		
		public function createBodyCanvas(bodyCanvas : Canvas , tree : Tree) : void {
			
			headGrid = new DataGrid();
			headGrid.name = tree.selectedItem.nodeid + "_headGrid";
			headGrid.right = 5;
			headGrid.left = 5;
			headGrid.top = 5;
			headGrid.percentHeight = 28;
			headGrid.doubleClickEnabled = true;
			headGrid.addEventListener(ListEvent.CHANGE , headItemChange);
			headGrid.addEventListener(ListEvent.ITEM_DOUBLE_CLICK , headItemDoubleClick);
			
			// 获取主表字段信息
			var tableServer : RemoteObject = new RemoteObject;
			tableServer.destination = IProcessor.TableProcessor;
			tableServer.resultTableJSON(tree.selectedItem.nodeid , "head");
			tableServer.addEventListener(ResultEvent.RESULT , headGridResult);
			
			bodyGrid = new DataGrid();
			bodyGrid.name = tree.selectedItem.nodeid + "_bodyGrid";
			bodyGrid.left = 5;
			bodyGrid.right = 5;
			bodyGrid.bottom = 5;
			bodyGrid.percentHeight = 69;
			
			// 获取子表字段信息
			tableServer = new RemoteObject;
			tableServer.destination = IProcessor.TableProcessor;
			tableServer.resultTableJSON(tree.selectedItem.nodeid , "body");
			tableServer.addEventListener(ResultEvent.RESULT , bodyGridResult);
			
			bodyCanvas.addChild(headGrid);
			bodyCanvas.addChild(bodyGrid);
			
		}
		
		public function leftTree_update(tree : Tree) : void {
			
			var parentName : String = tree.name.split(".")[0];
			sysCurSelectItem = tree.selectedItem;
			
			var nodeServer : RemoteObject = new RemoteObject;
			nodeServer.destination = IProcessor.NodeProcessor;
			nodeServer.resultNodeJSON(tree.selectedItem.nodecode);
			nodeServer.addEventListener(ResultEvent.RESULT , parentName == "system" ? sysChildResultHandler : childResultHandler);
			
		}
		
		public function sysChildResultHandler(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null ) {
				var result : Array = JSON.decode(lastResult) as Array;
				
				for each(var item : Object in result) {
					if(item.isparent == "Y" && item.children == null)
						item.children = "[]";
				}
				
				var childNodeArray : ArrayCollection = new ArrayCollection(result);
				
				sysCurSelectItem.children = childNodeArray;
				
				var dataProviderCollection : ArrayCollection = sysTree.dataProvider as ArrayCollection;
				
				sysCurSelectItem.isopen = true;
				
				replaceData(dataProviderCollection , sysCurSelectItem);
				
				sysTree.dataProvider = dataProviderCollection;
				
				openOrCloseItem(sysTree.dataProvider as ArrayCollection , sysTree);
				
			} 
		}
		
		public function childResultHandler(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null ) {
				var result : Array = JSON.decode(lastResult) as Array;
				
				for each(var item : Object in result) {
					if(item.isparent == "Y" && item.children == null)
						item.children = "[]";
				}
				
				var childNodeArray : ArrayCollection = new ArrayCollection(result);
				
				curSelectItem.children = childNodeArray;
				
				var dataProviderCollection : ArrayCollection = hrreport.ui.leftTree.dataProvider as ArrayCollection;
				
				curSelectItem.isopen = true;
				
				replaceData(dataProviderCollection , curSelectItem);
				
				hrreport.ui.leftTree.dataProvider = dataProviderCollection;
				
				openOrCloseItem(hrreport.ui.leftTree.dataProvider as ArrayCollection , hrreport.ui.leftTree);
				
			} 
		}
		
		public function openOrCloseItem(items : ArrayCollection , tree : Tree) : void {
			
			tree.callLater(function ():void {
				
				for each(var item:Object in items)  {
					
					tree.expandItem(item,item.isopen == null ? false : Boolean(item.isopen));
					if(item.children != null && item.children != "[]" && ArrayCollection(item.children).length > 0)
						openOrCloseItem(item.children , tree);
					
				}
				
			});
		}
		
		public function replaceData(dataProviderCollection : ArrayCollection , curSelectItem : Object) : void {
			
			var row : int = 0;
			for each(var data : Object in dataProviderCollection) {
				
				if(data.nodename == curSelectItem.nodename) {
					data = curSelectItem;
					return ;
				}
				
				if(data.children != null && data.children != "[]" && data.children.length > 0) {
					replaceData(data.children , curSelectItem);
				}
				
				row ++;
				
			}
			
		}
		
		public function headItemChange(event : ListEvent) : void {
			
			var parentName : String = event.target.name.split("_")[0];
			var selectItem : Object = DataGrid(event.target).selectedItem;
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			var headCard : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_headCard"));
			
			TextInput(headCard.getChildByName(parentName + "_txtPeriod")).text = selectItem.period;
			TextInput(headCard.getChildByName(parentName + "_txtBillType")).text = selectItem.billtype;
			
			var param : Object = ReportCommand.getInstance(parentName).initParam("headItemChange");
			var headCanvas : Canvas = param.headCanvas as Canvas;
			
			if(headCanvas != null) {
				Button(headCanvas.getChildByName(parentName + "_btnDelete")).enabled = true;
			}
			
			// 重置查询框的name，防止后续事件导致数据混乱
			//			titleWindow.name = parentName + "_QueryWindow";
			
			if(parentName == "joinpsn.corp") {
				PopUpManager.addPopUp(hrreport.waitWindow , hrreport.ui , true);
				PopUpManager.centerPopUp(hrreport.waitWindow);
			}
			
			var tableServer : RemoteObject = new RemoteObject();
			tableServer.destination = IProcessor.TableProcessor;
			tableServer.queryBodyData(selectItem.pk_statistic , parentName);
			tableServer.addEventListener(ResultEvent.RESULT , ReportCommand.getInstance(parentName).queryBodyData);
			
		}
		
		// 一次性读取书节点，过滤空子节点
		//		public function removeNullChildren(node : Object) : void {
		//			
		//			if(node.children == "[]" || node.children == "") {
		//				node.children = null;
		//				return ;	
		//			}
		//			
		//			for each(var nextNode : Object in node.children) {
		//				removeNullChildren(nextNode);
		//			}
		//			
		//		}
		
		// 初始化主表字段
		public function headGridResult(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			if(lastResult != null ) {
				var result : Array = JSON.decode(lastResult) as Array;
				var headArray : ArrayCollection = new ArrayCollection(result);
				
				for each(var head : Object in headArray) {
					if(head.pos == "head") {
						var column : DataGridColumn = new DataGridColumn();
						column.headerText = head.fieldname;
						column.dataField =  head.fieldcode;
						headGrid.columns = headGrid.columns.concat(column);
					}
				}
				
			} else {
				var parentName : String = headGrid.name.split("_")[0];
				var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
				headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
				ButtonEventHandler.getInstance().updateButtons(new MouseEvent(MouseEvent.CLICK) , '' , headCanvas , true);
				
			}
			
		}
		
		// 初始化子表字段 
		public function bodyGridResult(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			if(lastResult != null ) {
				var result : Array = JSON.decode(lastResult) as Array;
				var headArray : ArrayCollection = new ArrayCollection(result);
				
				for each(var body : Object in headArray) {
					if(body.pos == "body") {
						var column : DataGridColumn = new DataGridColumn();
						column.headerText = body.fieldname;
						column.dataField =  body.fieldcode;
						bodyGrid.columns = bodyGrid.columns.concat(column);
					}
				}
			} else {
				var parentName : String = headGrid.name.split("_")[0];
				var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
				headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
				ButtonEventHandler.getInstance().updateButtons(new MouseEvent(MouseEvent.CLICK) , '' , headCanvas , true);
			}
			
		}
		
		
		public function leftTree_init() : void {
			
			var nodeServer : RemoteObject = new RemoteObject;
			nodeServer.destination = IProcessor.NodeProcessor;
			nodeServer.resultNodeJSON("1");
			nodeServer.addEventListener(ResultEvent.RESULT , resultHandler);
			
		}
		
		
		public function leftTree_itemOpeningHandler(event:TreeEvent):void {
			
			
			var tree : Tree = Tree(event.target);
			
			if(event.opening) {
				tree.selectedItem = event.item;
				leftTree_itemDoubleClickHandler(event);
				
				if(event.item.children == "[]") {
					tree.callLater(function () : void {
						tree.expandItem(event.item , false);
					});
				}
			}
		}
		
		public function headItemDoubleClick(event : ListEvent) : void {
			ButtonEventHandler.getInstance().onBoCard(event);
		}
		
	}
}